﻿using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System.IO;
namespace MVCDemo.Web.Controllers
{
    /// <summary>
    /// 文件上传下载demo
    /// </summary>
    public class FileController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="env"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public string Upload([FromServices]IHostingEnvironment env, IFormFile file)
        {
            string result = string.Empty;
            if (file.Length > 0)
            {
                string filename = Path.Combine("upload/", string.Format("{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(file.FileName)));

                string filePath = Path.Combine(env.WebRootPath, filename);

                using (var stream = new FileStream(filePath, FileMode.CreateNew))
                {
                    file.CopyTo(stream);
                }
                result = filename;

            }
            return result;
        }
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="_hostingEnvironment"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public IActionResult Download([FromServices]IHostingEnvironment _hostingEnvironment, string file)
        {
            string DownLoadFilePath = Path.Combine(_hostingEnvironment.WebRootPath, file);
            try
            {
                //文件读取
                var stream = new FileStream(DownLoadFilePath, FileMode.Open);
                return File(stream, "application/octet-stream", Path.GetFileName(file));
            }
            catch (Exception)
            {
                return Content("抱歉,您要下载的文件不存在!!!");
            }
        }

    }
}